//discovery.js
const util = require("../../utils/util.js")
Page({
  data: {
    navTab: ["时政参考", "理论学习", "制度园地", "精华"],
    currentNavtab: "0",
    imgUrls: [
      '../../images/24213.jpg',
      '../../images/24280.jpg',
      '../../images/1444983318907-_DSC1826.jpg'
    ],
    indicatorDots: false,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    feed: [],
    feed_length: 0
  },
  onLoad: function () {
    console.log('onLoad')
    var that = this
    //调用应用实例的方法获取全局数据
    this.refresh();
  },
  onShow:function(){
    this.refresh();
  },
  switchTab: function (e) {
    this.setData({
      currentNavtab: e.currentTarget.dataset.idx
    });
  },

  bindItemTap: function () {
    wx.navigateTo({
      url: '../answer/answer'
    })
  },
  bindQueTap: function (e) {
    console.log(e)
    var id = e.currentTarget.id;
    console.log("跳转question")
    console.log(id)
    wx.navigateTo({
      url: '../question/question?articleId='+id,
    })
  },
  upper: function () {
    wx.showNavigationBarLoading()
    this.refresh();
    console.log("upper");
    setTimeout(function () { wx.hideNavigationBarLoading(); wx.stopPullDownRefresh(); }, 1000);
  },
  lower: function (e) {
    wx.showNavigationBarLoading();
    var that = this;
    setTimeout(function () { wx.hideNavigationBarLoading(); that.nextLoad(); }, 1000);
    console.log("lower")
  },
  //scroll: function (e) {
  //  console.log("scroll")
  //},

  //网络请求数据, 实现刷新
  refresh0: function () {
    var index_api = '';
    util.getData(index_api)
      .then(function (data) {
        //this.setData({
        //
        //});
        console.log(data);
      });
  },

  //使用本地 fake 数据实现刷新效果
  refresh: function () {
        var feed_data = [];
        util.getData('getArticleList', { articleId: "" })
          .then(res => {
            console.log(res)
            if (res.data.success) {
              feed_data = res.data.list;
              this.setData({
                feed: feed_data,
                feed_length: feed_data.length
              });
            } else {
              wx.showToast({
                title: "程序内部错误，请稍后再试",
                icon: '',
                duration: 2000
              })
            }
          })
      },
      //使用本地 fake 数据实现继续加载效果
      nextLoad: function () {
        var next = util.discoveryNext();
        console.log("continueload");
        var next_data = next.data;
        this.setData({
          feed: this.data.feed.concat(next_data),
          feed_length: this.data.feed_length + next_data.length
        });
      }
});
